var MapUtil_Util = {
    nativeBind: Function.prototype.bind,
    nativeIndexOf: Array.prototype.indexOf,
    nativeForEach: Array.prototype.forEach,
    nativeMap: Array.prototype.map
}
var MapUtil = {
    slice: Array.prototype.slice,
    identity: function(value) {
        return value;
    },
    /**
     *  Js中对象相关的方法
     */

    /*
     *  判断是不是数字对象
     * */

    isNumber: function(obj) {
        return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
    },
    /*
     *  判断是不是数组
     * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
     * */
    isArray: Array.isArray || function(obj) {
        return Object.prototype.toString.call(obj) === '[object Array]';
    },
    isInObj: function(obj, property) {
        return (property in obj) && obj.hasOwnProperty(property);
    },
    isArguments: function(obj) {
        return !!(obj && hasOwnProperty.call(obj, 'callee'));
    },
    isString: function(obj) {
        return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
    },
    
    
    extend: function(obj) {
        MapUtil.each(MapUtil.slice.call(arguments, 1), function(source) {
          for (var prop in source) {
            if (source[prop] !== void 0) obj[prop] = source[prop];
          }
        });
        return obj;
    },


/*
 *  数据集合相关方法
 * */
    makeArray: function(args) {
        return MapUtil.slice.call(args);
    },
    toArray: function(iterable) {
        if (!iterable)                return [];
        if (iterable.toArray)         return iterable.toArray();
        if (MapUtil.isArray(iterable))      return iterable;
        if (MapUtil.isArguments(iterable))  return MapUtil.makeArray(iterable);
        return MapUtil.map(iterable, MapUtil.identity);
    },
    /*
     * 
     * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/foreach
     * */
    each: function(obj, iterator, context) {
        if (obj == null) {
            return;
        }
        var nativeForEach = MapUtil_Util.nativeForEach;
        if (nativeForEach && obj.forEach === nativeForEach) {
            obj.forEach(iterator, context);
        } else if ( MapUtil.isNumber(obj.length) ) {
            for (var i = 0, l = obj.length; i < l; i++) {
                iterator.call(context, obj[i], i, obj);
            }
        } else {
            for (var key in obj) {
                if ( obj.hasOwnProperty(key) ) {
                    iterator.call(context, obj[key], key, obj);
                }
            }
        }
    },
    /*
     * 
     * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
     * */
    map: function(obj, iterator, context) {
        var nativeMap = MapUtil_Util.nativeMap,
            ret = [];
        if (obj == null) {
            return ret;
        }
        if (nativeMap && obj.map === nativeMap) {
            return obj.map(iterator, context);
        }
        MapUtil.each(obj, function(value, index, list) {
            ret[ret.length] = iterator.call(context, value, index, list);
        });
        return ret;
    },
    /*
     * 
     * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexof
     * */
    indexOf: function(array, item, isSorted) {
        var nativeIndexOf = MapUtil_Util.nativeIndexOf;
        if (array == null) return -1;
        if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
        for (var i = 0, l = array.length; i < l; i++) {
            if (array[i] === item) return i;
        }
        return -1;
    },
    
    pluck: function(obj, key) {
        return MapUtil.map(obj, function(value){ return value[key]; });
    },
    
    sortBy: function(obj, iterator, context) {
        return _.pluck(_.map(obj, function(value, index, list) {
            return {
                value : value,
                criteria : iterator.call(context, value, index, list)
            };
        }).sort(function(left, right) {
            var a = left.criteria, b = right.criteria;
            return a < b ? -1 : a > b ? 1 : 0;
        }), 'value');
    },
    
    /*
     * 
     * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
     * */
    bind: function(func, obj) {
        var nativeBind = MapUtil_Util.nativeBind;
        var slice = Array.prototype.slice;
        if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
        var args = slice.call(arguments, 2);
        return function() {
            return func.apply(obj, args.concat(slice.call(arguments)));
        };
    },
    
    foreach: function(map,fun) {
		if(map && fun){
			for(var e in map){
				fun(e,map[e]);
			}
			
		}
	},
    map2Str: function(obj) {
        var ret = MapUtil.map(obj, function(value, key) {
            return key + ":" + value;
        });
		return ret.join("");
	}
    
}


